﻿using System;
using System.Text.RegularExpressions;
using EmbeddedSharp.Core;

namespace EmbeddedSharp.Scripting.Function.RegularExpressions {

    /// <summary>
    /// RegexMatch("
    /// </summary>
    public class RegexMatchNode : FuncNode {

        public RegexMatchNode()
            : base() {
            base.Value = "RegexMatch";
        }

        public override object Evaluate() {
            if (Parameters.Count < 2) {
                return new ArgumentException(
                    "Usage: RegexMatch(expr, regex);");
            }

            string pattern 
                = Convert.ToString(Parameters[1].Evaluate());
            string expr 
                = Convert.ToString(Parameters[0].Evaluate());

            Regex regex = new Regex(pattern);
            Match match = regex.Match(expr);

            return match.Success;
        }
    }
}
